package cn.anecansaitin.whimsy.common.item;

import cn.anecansaitin.whimsy.client.registers.ModSounds;
import cn.anecansaitin.whimsy.common.attachment.DoomfistSkill;
import cn.anecansaitin.whimsy.common.register.ModDataAttachments;
import cn.anecansaitin.whimsy.common.register.ModEffects;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.UseAnim;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.lwjgl.glfw.GLFW;

import java.util.UUID;

public class Doomfist extends Item {
    private final Multimap<Attribute, AttributeModifier> defaultModifiers;

    public Doomfist() {
        super(new Properties().stacksTo(1));
        ImmutableMultimap.Builder<Attribute, AttributeModifier> builder = ImmutableMultimap.builder();
        //这是为了在左键点击时不会摇晃手臂
        builder.put(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_UUID, "Weapon modifier", 100, AttributeModifier.Operation.ADDITION));
        //血量高点怎么了
        builder.put(Attributes.MAX_HEALTH, new AttributeModifier(UUID.fromString("EBD91603-DA70-DDD0-2779-955BFDC37B59"), "Weapon modifier", 20, AttributeModifier.Operation.ADDITION));
        //临时护盾的上限
        builder.put(Attributes.MAX_ABSORPTION, new AttributeModifier(UUID.fromString("814532AB-2F91-DD83-A159-D187D06168E3"), "Weapon modifier", 20, AttributeModifier.Operation.ADDITION));
        this.defaultModifiers = builder.build();
    }

    public void keyInput(LivingEntity livingEntity, ItemStack doomfist, int key, int action) {
        DoomfistSkill skills = getSkills(doomfist);

        switch (key) {
            //右键蓄力
            case GLFW.GLFW_MOUSE_BUTTON_RIGHT -> {
                switch (action) {
                    case InputConstants.RELEASE -> {
                        if (skills.rocketPunch.isCharging) {
                            int i = skills.rocketPunch.rechargeTick * 11 / 20;

                            if (i <= 0) {
                                skills.rocketPunch.resetting();
                            } else {
                                livingEntity.removeEffect(ModEffects.ROCKET_PUNCH_CHARGING.get());
                                livingEntity.addEffect(new MobEffectInstance(ModEffects.ROCKET_PUNCH.get(), i, skills.rocketPunch.isEnhanced ? 1 : 0));
                                skills.rocketPunch.chargeComplete();
                            }
                        }
                    }
                    case InputConstants.PRESS -> {
                        if (skills.powerBlock.isCasting) {//先判断是否在格挡，取消格挡
                            livingEntity.removeEffect(ModEffects.POWER_BLOCK.get());
                        }

                        if (skills.rocketPunch.tryUse()) {
                            playRocketPunchRechargeSound(0, (Player) livingEntity);
                        }
                    }
                }
            }
            //E裂地猛击
            case GLFW.GLFW_KEY_E -> {
                if (skills.seismicSlam.isCasting) {
                    livingEntity.removeEffect(ModEffects.SEISMIC_SLAM.get());
                } else if (skills.seismicSlam.tryUse()) {
                    if (skills.powerBlock.isCasting) {//先判断是否在格挡，取消格挡
                        livingEntity.removeEffect(ModEffects.POWER_BLOCK.get());
                    }

                    livingEntity.addEffect(new MobEffectInstance(ModEffects.SEISMIC_SLAM.get(), -1));
                }
            }
            //空格超级跳
            case GLFW.GLFW_KEY_SPACE -> {
                livingEntity.removeEffect(ModEffects.ROCKET_PUNCH.get());
                skills.rocketPunch.castingComplete();
            }
            //左Shift悍将格挡
            case GLFW.GLFW_KEY_LEFT_SHIFT -> {
                if (skills.powerBlock.isCasting) {
                    livingEntity.removeEffect(ModEffects.POWER_BLOCK.get());
                } else if (skills.powerBlock.tryUse()) {
                    livingEntity.addEffect(new MobEffectInstance(ModEffects.POWER_BLOCK.get(), 2 * 20));
                }
            }
            //左键停止蓄力/停止格挡
            case GLFW.GLFW_MOUSE_BUTTON_LEFT -> {
                if (skills.rocketPunch.isCharging) {
                    skills.rocketPunch.interrupt();
                    livingEntity.removeEffect(ModEffects.ROCKET_PUNCH_CHARGING.get());
                } else if (skills.powerBlock.isCasting) {
                    livingEntity.removeEffect(ModEffects.POWER_BLOCK.get());
                }
            }
        }
    }

    @Override
    public void inventoryTick(ItemStack stack, Level level, Entity entity, int slotId, boolean isSelected) {
        if (!level.isClientSide && entity instanceof Player player) {
            DoomfistSkill skills = getSkills(stack);
            skills.tick();

            //火箭重拳蓄力
            if (skills.rocketPunch.isInCharging()) {
                playRocketPunchRechargeSound(skills.rocketPunch.chargingTick, player);
                player.addEffect(new MobEffectInstance(ModEffects.ROCKET_PUNCH_CHARGING.get(), 10));
            } else if (skills.rocketPunch.isChargingComplete()) {
                player.removeEffect(ModEffects.ROCKET_PUNCH_CHARGING.get());
                player.addEffect(new MobEffectInstance(ModEffects.ROCKET_PUNCH.get(), skills.rocketPunch.rechargeTick * 11 / 20, skills.rocketPunch.isEnhanced ? 1 : 0));
                skills.rocketPunch.chargeComplete();
            }

            //临时护盾的衰减
            if (skills.ultimateEfficiency.shouldAttenuate()) {
                float amount = player.getAbsorptionAmount();
                player.setAbsorptionAmount(Math.max(0, amount - skills.ultimateEfficiency.attenuationRate));
            }
        }
    }

    @Override
    public boolean onEntitySwing(ItemStack stack, LivingEntity entity) {
        return true;
    }

    @Override
    public boolean canAttackBlock(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer) {
        return false;
    }

    @Override
    public boolean onLeftClickEntity(ItemStack stack, Player player, Entity entity) {
        return true;
    }

    @Override
    public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
        return false;
    }

    @Override
    public Multimap<Attribute, AttributeModifier> getAttributeModifiers(EquipmentSlot slot, ItemStack stack) {
        return slot == EquipmentSlot.MAINHAND ? defaultModifiers : super.getAttributeModifiers(slot, stack);
    }

    private DoomfistSkill getSkills(ItemStack stack) {
        return stack.getData(ModDataAttachments.DOOMFIST_SKILL);
    }

    @Override
    public UseAnim getUseAnimation(ItemStack pStack) {
        return super.getUseAnimation(pStack);
    }

    private void playRocketPunchRechargeSound(int chargingTick, Player player) {
        switch (chargingTick) {
            case 0 -> player.level().playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ROCKET_PUNCH_RECHARGE_1.get(), SoundSource.PLAYERS);
            case 8 -> player.level().playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ROCKET_PUNCH_RECHARGE_2.get(), SoundSource.PLAYERS);
            case 16 -> player.level().playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ROCKET_PUNCH_RECHARGE_3.get(), SoundSource.PLAYERS);
            case 24 -> player.level().playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ROCKET_PUNCH_RECHARGE_4.get(), SoundSource.PLAYERS);
            case 32 -> player.level().playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ROCKET_PUNCH_RECHARGE_5.get(), SoundSource.PLAYERS);
            case 40 -> player.level().playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ROCKET_PUNCH_RECHARGE_6.get(), SoundSource.PLAYERS);
            case 48 -> player.level().playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ROCKET_PUNCH_RECHARGE_7.get(), SoundSource.PLAYERS);
        }
    }
}
